Skip to content

fix(NODE-7548): SCRAM authentication fails on non-Node runtimes#4932

Merged
tadjik1 merged 23 commits into
mainfrom
node-7548-scram-deno
May 19, 2026
Merged

fix(NODE-7548): SCRAM authentication fails on non-Node runtimes#4932
tadjik1 merged 23 commits into
mainfrom
node-7548-scram-deno

Conversation

@PavelSafronov
Copy link
Copy Markdown
Contributor

@PavelSafronov PavelSafronov commented Apr 30, 2026

Description

Summary of Changes

This fixes the deno-specific issue where SCRAM is broken. The bug happens because we are invoking toString implicitly, and this results in a behavior change. The fix is to convert the bytes to string explicitly.

Part of the reason that this was not caught in testing is because we were still targeting node when creating the bundle.

Switching bundler to web exposed a few issues:

  • removed last few Buffer.readInt32LE calls
  • skipped more tests in nodeless where sinon can cause test failures
  • supply globals that are not available in web (TextDecoder, TextEncoder, atob, btoa)
  • export the created sandbox so our tests can verify that we aren't exporting things like Buffer
  • update tests that verify error types
  • update the bundle barrow file with missing exports
Notes for Reviewers

What is the motivation for this change?

Customer-created ticket. Driver is broken infor deno.

Release Highlight

Fixed SCRAM authentication for non-Node.js runtimes (e.g., Deno)

SCRAM-based authentication (the default mechanism for username/password connections) was broken when using the driver in non-Node.js environments such as Deno. The root cause was an implicit toString() call on byte arrays that produced incorrect output outside of Node.js. This fix ensures explicit UTF-8 string conversion is used throughout the SCRAM implementation, restoring authentication in Deno and other web-compatible runtimes.

Double check the following

  • Lint is passing (npm run check:lint)
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

PavelSafronov and others added 5 commits April 29, 2026 14:56
… call

also updated bundling logic to correctly remove Buffer. 9 tests are currently failing, with failures that are not related to scram.

Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
@PavelSafronov PavelSafronov marked this pull request as ready for review May 5, 2026 22:24
@PavelSafronov PavelSafronov requested a review from a team as a code owner May 5, 2026 22:24
Copilot AI review requested due to automatic review settings May 5, 2026 22:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses SCRAM authentication and unit test failures when running the driver in “nodeless” (non-Node) runtimes by replacing Node-specific Buffer APIs with runtime-neutral utilities, adjusting the bundling configuration, and conditionally adapting/skipping tests that rely on Node-only behavior.

Changes:

  • Update SCRAM message parsing/encoding to use ByteUtils UTF-8 helpers instead of Binary#toString('utf8').
  • Adjust unit tests to run in nodeless environments (conditional assertions, avoiding Buffer APIs, skipping sinon-dependent tests).
  • Change the driver bundling setup to platform: 'browser' and tighten the VM sandbox globals used by bundled tests.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/unit/utils.test.ts Makes compareObjectId tests conditional for nodeless behavior differences.
test/unit/sessions.test.ts Avoids instanceof Long checks in nodeless by using ensureTypeByName.
test/unit/nodeless.test.ts Adds assertions about the VM sandbox not exposing Node globals.
test/unit/commands.test.ts Replaces Buffer read methods with runtime-neutral helpers.
test/unit/cmap/wire_protocol/on_demand/document.test.ts Adapts throw assertions for nodeless runtime differences (needs tightening to avoid false positives).
test/unit/cmap/connect.test.ts Skips sinon-based socket tests when running nodeless.
test/unit/cmap/commands.test.ts Removes Buffer-specific toString usage for document sequence name assertions.
test/unit/client-side-encryption/state_machine.test.ts Skips sinon-based tests for nodeless runs.
test/tools/runner/vm_context_helper.ts Updates VM sandbox globals and adds bundle debugging hooks; exports sandbox.
test/mongodb_bundled.ts Exposes additional exports needed by bundled/nodeless tests (makeSocket, keep-alive constant).
src/cmap/auth/scram.ts Switches SCRAM auth message decoding/parsing to ByteUtils.toUTF8.
src/bson.ts Adds readUint8 helper alongside existing buffer read helpers (needs bounds validation).
etc/bundle-driver.mjs Bundles with platform: 'browser'/chrome112 and bundles bson instead of externalizing it.
Comments suppressed due to low confidence (1)

src/cmap/auth/scram.ts:213

  • parsePayload currently decodes payload.buffer using payload.buffer.length, which can include unwritten bytes (see Binary.position). It also assumes payload is always a Binary, but other code already handles response.payload being a Uint8Arrayr.payload may be as well. Consider normalizing inside parsePayload (accept Binary | Uint8Array) and decode only the actual payload length.
function parsePayload(payload: Binary) {
  const payloadStr = ByteUtils.toUTF8(payload.buffer, 0, payload.buffer.length, true);
  const dict: Document = {};
  const parts = payloadStr.split(',');
  for (let i = 0; i < parts.length; i++) {
    const valueParts = (parts[i].match(/^([^=]*)=(.*)$/) ?? []).slice(1);
    dict[valueParts[0]] = valueParts[1];
  }

Comment thread src/cmap/auth/scram.ts Outdated
Comment thread src/bson.ts
Comment thread test/unit/cmap/wire_protocol/on_demand/document.test.ts
Comment thread test/unit/cmap/wire_protocol/on_demand/document.test.ts
Comment thread test/unit/cmap/wire_protocol/on_demand/document.test.ts
Comment thread test/unit/cmap/wire_protocol/on_demand/document.test.ts Outdated
@PavelSafronov PavelSafronov marked this pull request as draft May 5, 2026 22:31
@PavelSafronov PavelSafronov marked this pull request as ready for review May 5, 2026 22:42
- use Binary.position instead of buffer.length
- add a validateBufferInputs call to readUint8
- add expect.fail in cases where we expect an exception
@tadjik1 tadjik1 self-assigned this May 6, 2026
@tadjik1 tadjik1 added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label May 6, 2026
Comment thread src/cmap/auth/scram.ts Outdated
Comment thread test/unit/utils.test.ts
Copy link
Copy Markdown
Member

@tadjik1 tadjik1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @PavelSafronov! I left few small questions, please let me know what you think.
Can you also please take a look at lint errors?

And I guess you spotted another bug by switching the environment:

[2026/05/06 01:26:35.258]   34) Unified test format runner (valid-pass)
[2026/05/06 01:26:35.258]        poc-sessions
[2026/05/06 01:26:35.258]          Server supports implicit sessions:
[2026/05/06 01:26:35.258]      TypeError: [last.command.lsid.id](http://last.command.lsid.id/).buffer.equals is not a function
[2026/05/06 01:26:35.258]       at assertSameLsidOnLastTwoCommands (test/tools/unified-spec-runner/operations.ts:147:38)
[2026/05/06 01:26:35.258]       at executeOperationAndCheck (test/tools/unified-spec-runner/operations.ts:1037:20)
[2026/05/06 01:26:35.258]       at runUnifiedTest (test/tools/unified-spec-runner/runner.ts:239:39)
[2026/05/06 01:26:35.258]       at processTicksAndRejections (node:internal/process/task_queues:104:5)
[2026/05/06 01:26:35.258]       at async Context.<anonymous> (test/tools/unified-spec-runner/runner.ts:349:13)[2026/05/06 01:26:35.258]   34) Unified test format runner (valid-pass)
[2026/05/06 01:26:35.258]        poc-sessions
[2026/05/06 01:26:35.258]          Server supports implicit sessions:
[2026/05/06 01:26:35.258]      TypeError: [last.command.lsid.id](http://last.command.lsid.id/).buffer.equals is not a function
[2026/05/06 01:26:35.258]       at assertSameLsidOnLastTwoCommands (test/tools/unified-spec-runner/operations.ts:147:38)
[2026/05/06 01:26:35.258]       at executeOperationAndCheck (test/tools/unified-spec-runner/operations.ts:1037:20)
[2026/05/06 01:26:35.258]       at runUnifiedTest (test/tools/unified-spec-runner/runner.ts:239:39)
[2026/05/06 01:26:35.258]       at processTicksAndRejections (node:internal/process/task_queues:104:5)
[2026/05/06 01:26:35.258]       at async Context.<anonymous> (test/tools/unified-spec-runner/runner.ts:349:13)[2026/05/06 01:26:35.258]   34) Unified test format runner (valid-pass)
[2026/05/06 01:26:35.258]        poc-sessions
[2026/05/06 01:26:35.258]          Server supports implicit sessions:
[2026/05/06 01:26:35.258]      TypeError: [last.command.lsid.id](http://last.command.lsid.id/).buffer.equals is not a function
[2026/05/06 01:26:35.258]       at assertSameLsidOnLastTwoCommands (test/tools/unified-spec-runner/operations.ts:147:38)
[2026/05/06 01:26:35.258]       at executeOperationAndCheck (test/tools/unified-spec-runner/operations.ts:1037:20)
[2026/05/06 01:26:35.258]       at runUnifiedTest (test/tools/unified-spec-runner/runner.ts:239:39)
[2026/05/06 01:26:35.258]       at processTicksAndRejections (node:internal/process/task_queues:104:5)
[2026/05/06 01:26:35.258]       at async Context.<anonymous> (test/tools/unified-spec-runner/runner.ts:349:13)

TypeError: last.command.lsid.id.buffer.equals is not a function

Comment thread src/cmap/auth/scram.ts Outdated
Comment thread test/tools/utils.ts Outdated
Copy link
Copy Markdown
Member

@tadjik1 tadjik1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @PavelSafronov, great work! I have left, I believe, 2 last small comments, to preserve existing behaviour and small typo fix. Everything else is looking really good 👍

- avoid throwing by toUTF8
- clarify comments
tadjik1
tadjik1 previously approved these changes May 8, 2026
Copy link
Copy Markdown
Member

@tadjik1 tadjik1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

will merge on complete CI run

Comment thread src/cmap/auth/scram.ts
Comment thread test/integration/client-side-encryption/driver.test.ts
Comment thread test/tools/runner/vm_context_helper.ts Outdated
Copy link
Copy Markdown
Contributor

@nbbeeken nbbeeken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anna's comments echo my questions I had during review ^^

- remove unnecessary changes
…ked to ticket to fix that behavior in js-bson
@tadjik1 tadjik1 merged commit a10d2c9 into main May 19, 2026
32 checks passed
@tadjik1 tadjik1 deleted the node-7548-scram-deno branch May 19, 2026 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Primary Review In Review with primary reviewer, not yet ready for team's eyes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants